package utils;


import java.sql.*;

public class DbUtil {
    public static final String URL = "jdbc:mysql://localhost:3306/vendingmachine";
    public static final String USER = "root";
    public static final String PASSWORD = "root";
    private static Connection conn = null;
    static{
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void touchUser(long userId) throws SQLException {
        String sql = "UPDATE user SET last_action_time = NOW() WHERE id = ?";
        Connection c = DbUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);
        ps.setLong(1, userId);
        ps.executeUpdate();
    }
    public static void touchAdmin(long adminId) throws SQLException {
        String sql = "UPDATE admin SET last_action_time = NOW() WHERE id = ?";
        Connection c = DbUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);
        ps.setLong(1, adminId);
        ps.executeUpdate();
    }

    public static Connection getConnection(){
        return conn;
    }
}
